package com.leetcode;

/**
 * 66. 加一
 *
 * @author fy
 * @date 2022/4/20 22:03
 */
public class Solution66 {

    public int[] plusOne(int[] digits) {
        int n = digits.length;
        int in = 0;
        for (int i = n - 1; i >= 0; i--) {
            int num = digits[i] + in;
            if (i == n - 1) {
                 num += 1;
            }
            in = num / 10;
            digits[i] = num % 10;
            if (in == 0) {
                break;
            }
        }
        if (in > 0) {
            int[] tmp = new int[n + 1];
            tmp[0] = in;
            for (int i = 0; i < n; i++) {
                tmp[i + 1] = digits[i];
            }
            return tmp;
        }
        return digits;
    }

    public static void main(String[] args) {
        int[] digits = new int[]{1, 2, 3};
        int[] res = new Solution66().plusOne(digits);
        for (int i = 0; i < res.length; i++) {
            System.out.print(res[i] + " ");
        }
        System.out.println();
    }

}
